function ip = getIntersec(fm)
%GETINTERSEC returns the intersection point IP between the dart and the
% board using foreground mask FM.

% Project:  Dart detection [Bildfolgen LU]@CVL/TUWIEN
% Author:   Thomas Rittler
% Date:     05/2011

tmprp = regionprops(fm, 'Centroid', 'Orientation')
if size(tmprp,1) > 0
    if  abs(tmprp(1).Orientation) < 45
        ip(1) = 0;
        ip(2) = 0;
        return;
    else
        theta = 360 - tmprp(1).Orientation;
    end
    se = strel('line', 10, theta);
    fm = imerode(fm, se);
    rp = regionprops(fm, 'Area', 'Centroid', 'Orientation');
    area = [rp.Area];
    idx = find(area == max(area));
    if numel(tmprp) == 1 && numel(rp)>1 % if FM breaks into fragments
        rp = tmprp;
        idx = 1;
    end

    % intersection line
    x = -1000:1:1000;
    y = ones(1, numel(x));
    % centroid
    xm = rp(idx).Centroid(1);
    ym = rp(idx).Centroid(2);
    % angle
    theta = 360 - rp(idx).Orientation;
    % rotation matrix
    R = [cosd(theta) sind(theta); -sind(theta) cosd(theta)];

    ln = [x' y']*R;
    ln(:,1) = ln(:,1)+xm;
    ln(:,2) = ln(:,2)+ym;

    % row = find(sum(fm,2),1,'last');         % y-coordinate
    % ip = mean(ln(round(ln(:,2))==row, 1));  % x-coordinate

    %-----------
    ip(2) = find(sum(fm,2),1,'last');            % y-coordinate
    ip(1) = mean(ln(round(ln(:,2))==ip(2), 1));  % x-coordinate
else
    ip(1) = 0;
    ip(2) = 0;
end
end

